
package com.mapsolute.map24webservices15.clients.axis.wrappers;

import com.mapsolute.map24webservices15.clients.axis.stubs.*;

/**
 *  The Map24 Routing Service calculates a route between two locations and, optionally, via additional stops. 
 * The generated route information consists of a graphical representation (polyline) of the route and a turn 
 * by turn detailed route description. The mapping client can render the geographical representation of the 
 * route together with the map data so that the route is displayed on the map.
 *
 * The Map24 Routing Service is flexible in parameterization. The application developer can choose a 
 * preconfigured set of parameters for speed and other driver preferences. In addition, a sophisticated 
 * lower level access to the input parameters is provided that allows to fine tune the routing algorithm 
 * and to add application specific routing behavior.
 * 
 * The routing attributes that are considered by a Map24 Routing Service depend in some respect on the 
 * underlying digital map content. The following routing attributes are usually supported from all digital 
 * map providers and are therefore considered by the Map24 Routing Service: functional road class, form of 
 * way, route numbers, direction of traffic flow (one-way, two-way, divided highway), road condition, network 
 * classifications, blocked passages, special restrictions, restricted maneuvers, toll roads, etc.
 *
 * This class provides a wrapper for parts of the functionality of the Map24 Routing Service.
 */
public class Map24RoutingServiceWrapper 
{
	
	/** 
	 * Helper method for instantiating a Coordinate object.
	 * 
	 * @param longitude The coordinate's longitude.
	 * @param latitude The coordinate's latitude.
	 * @return	An instance of type Coordinate.
	 */
	public static Coordinate Coordinate(double longitude, double latitude)
	{
		Coordinate coordinate= new Coordinate();
		coordinate.setLongitude (longitude);
		coordinate.setLatitude  (latitude);
		
		return coordinate;
		
	} // Coordinate
	
	/** 
	 * Helper method for instantiating an MProperty object.
	 * 
	 * MProperty objects are used for setting multiple properties (key-value pairs).
	 * 
	 * @param key  	The property key.
	 * @param value The value.
	 * @return			An instance of MProperty.
	 */
	public static MProperty MProperty(String key, String value)
	{
		MProperty prop= new MProperty();
		
		prop.setKey 	(key);
		prop.setValue  (value);
		
		return prop;
		
	} // Coordinate
	
	/** 
	 * Helper method for adding a property to a calculateRoute request.
	 * 
	 * @param key 		The propertie's key.
	 * @param value 	The propertie's value.
	 * @param request An output parameter that will contain the request, containing the 
	 * property in an instance of type MProperty.
	 */
	public static void AddPropertyToCalculateRouteRequest(String key, String value, CalculateRouteRequest request)
	{
		MProperty[] props= new MProperty[1 + ((request.getProperties() == null) ? 0 : request.getProperties().length)];
		
		for (int i= 1; (i < props.length); i++)
		  props[i]= request.getProperties()[i-1];
		
		props[0]= MProperty(key, value);
			
		request.setProperties(props);
		
	} // AddPropertyToCalculateRouteRequest
	
	/** 
	 * Helper method for generating instances of type RoutingRequestSpeedClassification.
	 *
	 * The speed classification's unit is km/h.
	 *
	 * @param speedOnMotorways 		The estimated speed on motorways.
	 * @param speedOnCountryRoads The estimated speed on country roads.
	 * @param speedOnCityRoads 		The estimated speed on city roads.
	 * @param speedOnFerries 			The estimated speed of ferries.
	 * @return An instance of type RoutingRequestSpeedClassification.
	 */
	public static RoutingRequestSpeedClassification RoutingRequestSpeedClassification(int speedOnMotorways, int speedOnCountryRoads, int speedOnCityRoads, int speedOnFerries)
	{
		RoutingRequestSpeedClassification spec= new RoutingRequestSpeedClassification();
		
		spec.setMotorway	(speedOnMotorways);
		spec.setCountryRoad	(speedOnCountryRoads);
		spec.setCityRoad	(speedOnCityRoads);
		spec.setFerry		(speedOnFerries);
		
		return spec;
		
	} // RoutingRequestSpeedClassification
	
	
	private static Map24RoutingPortType GetMap24RoutingPort(String binding, String map24id, String clientid) throws Exception
	{ 	
		Map24RoutingLocator locator= new Map24RoutingLocator();
				
		// DEBUG: try to create a port with other URL (e.g. proxy connection)
		if (binding == null)
			return locator.getMap24RoutingPort();
		
		java.net.URL url = new java.net.URL(binding+"&sid="+clientid);
			
	    //set Call properties
		// for shrinking Document sizes
		Map24RoutingPortType port= locator.getMap24RoutingPort(url);
		
		if (port instanceof Map24RoutingBindingStub)
		{

			((Map24RoutingBindingStub)port)._setProperty(org.apache.axis.client.Call.SEND_TYPE_ATTR,  Boolean.FALSE);
			((Map24RoutingBindingStub)port)._setProperty(org.apache.axis.AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);
			((Map24RoutingBindingStub)port)._setProperty(org.apache.axis.AxisEngine.PROP_SEND_XSI,    Boolean.FALSE);
		}
				
		
		
		return port;
			
	 } // GetMap24RoutingPort
	
	
	/** 
	  * Helper method for instantiating a CalculateRouteRequest.
	  *
	  * The default settings for all flags (e.g. CalculationMode) are used. 
	  * See the documentation of the XML/SOAP interface of the Map24 Routing Service.
	  * 
	  * @param start 				The coordinate of the route's start.
	  * @param destination 	The coordinate of the route's destination.
	  * @return An instance of CalculateRouteRequest.
	  */
	public static CalculateRouteRequest CalculateRouteRequest(Coordinate start, Coordinate destination)
	{
		CoordinateAndAddress START= new CoordinateAndAddress();
		START.setCoordinate(start);
		
		CoordinateAndAddress DESTINATION= new CoordinateAndAddress();
		DESTINATION.setCoordinate(destination);
		
   		CalculateRouteRequest request = new CalculateRouteRequest();
   		request.setStart(START);
   		request.setDestination(DESTINATION);
   		
   		return request;	
   		
	} // CalculateRouteRequest
	
	
	
	/** 
	  * Wrapper method for calculating a route.
	  *
	  * @param binding The service that is used, for example ""http://maptp12.map24.com/map24/webservices1.5?soap=Map24Routing". 
	  *                 If null, the server URL specified in the client stub Map24RoutingLocator is used.
	  * @param map24id The Map24 ID used for authentication.
	  * @param clientid ID of the client that has initiated a request (SessionID).
	  * @param request The CalculateRouteRequest object containing the start and destination of the route.
	  * @return 			 An instance of CalculateRouteResponse containing the routing data as result.
	  */
	public static CalculateRouteResponse CalculateTimeDistance(String binding, String map24id, String clientid, CalculateRouteRequest request)
	{		
		return CalculateRoute(binding, map24id, clientid, request);
	
	} // CalculateRoute
	
  /**
  * Wrapper method for calculating a route. Only the Route ID is returned. The complete route information can be received in a
  * subsequent request.
  *
  * @param binding  The service that is used, for example ""http://maptp12.map24.com/map24/webservices1.5?soap=Map24Routing". 
  *                 If null, the server URL specified in the client stub Map24RoutingLocator is used.
  * @param map24id  The Map24 ID used for authentication.
  * @param clientid ID of the client that has initiated a request (SessionID).
  * @param start    The start of the route.
	* @param destination The destination of the route.
  * @return 			  An instance of CalculateRouteResponse containing the routing data as result.
  */
	public static CalculateRouteResponse CalculateTimeDistance(String binding, String map24id, String clientid, Coordinate start, Coordinate destination)
	{		
		return CalculateRoute(binding, map24id, clientid, start, destination, true, true);
	
	} // CalculateRoute
	
	/**
	* Wrapper method for calculating a route. The route information is returned in the response.
	*
	* @param binding  The service that is used, for example ""http://maptp12.map24.com/map24/webservices1.5?soap=Map24Routing". 
	*                 If null, the server URL specified in the client stub Map24RoutingLocator is used.
	* @param map24id  The Map24 ID used for authentication.
	* @param clientid ID of the client that has initiated a request (SessionID).
	* @param start    The start of the route.
	* @param destination The destination of the route.
	* @return 			  An instance of CalculateRouteResponse containing the routing data as result.
	*/
	public static CalculateRouteResponse CalculateRoute(String binding, String map24id, String clientid, Coordinate start, Coordinate destination)
	{		
		return CalculateRoute(binding, map24id, clientid, start, destination, false, false);
	
	} // CalculateRoute
	
	
	/**
	* Calculates a route, allowing to pass values for the IgnoreDescription and IgnoreWaypoints flags.
	*
	* @param binding   The service that is used, for example ""http://maptp12.map24.com/map24/webservices1.5?soap=Map24Routing". 
	*                  If null, the server URL specified in the client stub Map24RoutingLocator is used.
	* @param map24id   The Map24 ID used for authentication.
	* @param clientid  ID of the client that has initiated a request (SessionID).
	* @param start		 Start of the route.
	* @param destination	Destination of the route.
	* @param ignoreDescription	If TRUE, only the RouteID is returned. If FALSE (the default), the RouteID and a 
	* detailed route description is returned. The RouteID is needed to identify the route information on the server 
	* in subsequent requests.
	* @param ignoreWaypoints The coordinates that describe the geometry of the route are called way items.
	* If this field is set to FALSE (the default), all way items of the route are returned in the response.
	* When setting this field to TRUE, only one coordinate per route segment is returned. This option can 
	* for example be used to center on a route segment or to show a logo for each route segment. Note that 
	* it is not necessary to set this flag to FALSE for showing the route on the map, since the coordinates for 
	* showing the route are received from the server.
	* @return An instance of CalculateRouteResponse containing the routing data as result.
	*/
	public static CalculateRouteResponse CalculateRoute(String binding, String map24id, String clientid, Coordinate start, Coordinate destination, boolean ignoreDescription, boolean ignoreWaypoints)
	{
   		CalculateRouteRequest request = CalculateRouteRequest(start, destination);
   		
   		request.setIgnoreDescription(ignoreDescription);
   		request.setIgnoreWaypoints(ignoreWaypoints);
   		request.setLoadTMCs(false);
   		request.setCalculationMode(RouteCalculationMode.Fastest);
   		request.setVehicleType(RoutingRequestVehicleType.Car);
   		request.setLoadTMCs(false);
   		request.setMinQualityOfGeocodeResults(GeocodedAddressQuality.Street);
   		request.setMaxNoOfGeocodeAlternatives(1);
   		
		return CalculateRoute(binding, map24id, clientid, request);
	
   } // CalculateRoute
	
  /**
	* Calculates a route from a given CalculateRouteRequest object.
	*
	* @param binding   The service that is used, for example ""http://maptp12.map24.com/map24/webservices1.5?soap=Map24Routing". 
	*                  If null, the server URL specified in the client stub Map24RoutingLocator is used.
	* @param map24id   The Map24 ID used for authentication.
	* @param clientid  ID of the client that has initiated a request (SessionID).
	* @param request 	 An instance of CalculateRouteRequest containing the request parameters. 
	*                  The CalculateRouteRequest object must at least contain the start and destination of the route.
	* @return 				 An instance of CalculateRouteResponse containing the routing data as result.
	*/
	public static CalculateRouteResponse CalculateRoute(String binding, String map24id, String clientid, CalculateRouteRequest request)
	{

		RequestHeader header = new RequestHeader();
		header.setMap24ID  (map24id);
		header.setClientID (clientid);
			
		return CalculateRoute(binding, header, request);
						
   } // CalculateRoute
	
	/**
	* Calculates a route using the given RequestHeader and CalculateRouteRequest objects.
	*
	* @param binding   The service that is used, for example ""http://maptp12.map24.com/map24/webservices1.5?soap=Map24Routing". 
	*                  If null, the server URL specified in the client stub Map24RoutingLocator is used.
	* @param header		 An instance of RequestHeader containing the Map24 ID used for authentication and the client ID.
	* @param request 	 An instance of CalculateRouteRequest containing the request parameters. 
	*                  The CalculateRouteRequest object must at least contain the start and destination of the route.
	* @return 				 An instance of CalculateRouteResponse containing the routing data as result.
	*/
	public static CalculateRouteResponse CalculateRoute(String binding, RequestHeader header, CalculateRouteRequest request)
	{				
		try 
		{	return GetMap24RoutingPort(binding, header.getMap24ID(), header.getClientID()).calculateRoute(header, request);		
		}
		catch(Exception e)
		{
			System.err.println(e);
		}
		
		return null;
	
   } // CalculateRoute
	
	
	/** Helper method for instantiating a FetchRouteLinksRequest.
	  * 
	  * @param binding   The service that is used, for example ""http://maptp12.map24.com/map24/webservices1.5?soap=Map24Routing". 
	  *                  If null, the server URL specified in the client stub Map24RoutingLocator is used.
	  * @param Map24ID   The Map24 ID used for authentication.
	  * @param ClientID  ID of the client that has initiated a request (SessionID).
	  * @param RouteID 	 ID of a previously calculated route. 
	  * @return 				 An instance of CalculateRouteResponse containing the coordinates of 
	  *                  each route segment of a calculated route.
	  */
	public static FetchRouteLinksResponse fetchRouteLinks(String binding, String Map24ID, String ClientID, String RouteID)
	{
	RequestHeader header = new RequestHeader();
	header.setMap24ID  (Map24ID);
	header.setClientID (ClientID);

	FetchRouteLinksRequest request = new FetchRouteLinksRequest();
	request.setRouteID(RouteID);


	try 
	{return GetMap24RoutingPort(binding, header.getMap24ID(), header.getClientID()).fetchRouteLinks(header, request);
	}
	catch(Exception e)
	{
	System.err.println(e);
	}

	return null;

	   } // fetchRoute
	
	/** 
	  * Helper method for instantiating a FetchRouteRequest.
	  * 
	  * @param binding   The service that is used, for example ""http://maptp12.map24.com/map24/webservices1.5?soap=Map24Routing". 
	  *                  If null, the server URL specified in the client stub Map24RoutingLocator is used.
	  * @param Map24ID   The Map24 ID used for authentication.
	  * @param ClientID  ID of the client that has initiated a request (SessionID).
	  * @param RouteID 	 ID of a previously calculated route. 
	  * @return 				 An instance of CalculateRouteResponse containing complete routing data of the specified route. 
	  */
	public static FetchRouteResponse fetchRoute(String binding, String Map24ID, String ClientID, String RouteID)
	{
	RequestHeader header = new RequestHeader();
	header.setMap24ID  (Map24ID);
	header.setClientID (ClientID);

	FetchRouteRequest request = new FetchRouteRequest();
	request.setRouteID(RouteID);


	try 
	{return GetMap24RoutingPort(binding, header.getMap24ID(), header.getClientID()).fetchRoute(header, request);
	}
	catch(Exception e)
	{
	System.err.println(e);
	}

	return null;

	   } // fetchRoute

	/**
	 * Helper method for instantiating a CalculateRouteRequest, passing many routing parameters.
	 * 
	 * @param binding   The service that is used, for example ""http://maptp12.map24.com/map24/webservices1.5?soap=Map24Routing". 
	 *                  If null, the server URL specified in the client stub Map24RoutingLocator is used.
	 * @param map24id   The Map24 ID used for authentication.
	 * @param clientid  ID of the client that has initiated a request (SessionID).
	 * @param calculationMode	The mode for the route calculation. This may be either "shortest" or "fastest"  
	 * and is "fastest" by default. Be aware that the shortest route must not necessarily be the fastest.
	 * @param vehicleType Type of the vehicle with which the route is traversed. The value can be either "Car" 
	 * or "Pedestrian". It is "Car" by default. In case of "Pedestrian" motorways are not used. Furthermore, 
	 * one-way rules and other direction instructions are ignored in this case.
	 * @param start       Start of the route.
	 * @param destination Destination of the route.
	 * @param viaPoints   Optional: An array containing the via items used to define the course of the route.
	 * @param descriptionLanguage Language of the route description. Use the two-letter language code as specified 
	 * in ISO 639-1. The following languages are supported: Czech (cs), German (de), Danish (da), English (en), 
	 * Spanish (es), Finnish (fi), French (fr), Croatian (hr), Hungarian (hu), Icelandic (is), Italian (it), 
	 * Dutch (nl), Norwegian (no), Polish (pl), Portuguese (pt), Romanian (ro), Russian (ru), Swedish (sv), Turkish (tr).
	 * @param ignoreDescription	If TRUE, only the RouteID is returned. If FALSE (the default), the RouteID and a 
	 * detailed route description is returned. The RouteID is needed to identify the route information on the server 
	 * in subsequent requests.
	 * @param ignoreWaypoints The coordinates that describe the geometry of the route are called way items.
	 * If this field is set to FALSE (the default), all way items of the route are returned in the response.
	 * When setting this field to TRUE, only one coordinate per route segment is returned. This option can 
	 * for example be used to center on a route segment or to show a logo for each route segment. Note that 
	 * it is not necessary to set this flag to FALSE for showing the route on the map, since the coordinates for 
	 * showing the route are received from the server.
	 * @return An instance of CalculateRouteResponse containing the result.
	 */
	public static CalculateRouteResponse CalculateRoute(String binding, String map24id, String clientid, RouteCalculationMode calculationMode, RoutingRequestVehicleType vehicleType, Coordinate start, Coordinate destination, CalculateRouteRequestViaPoint[] viaPoints, String descriptionLanguage, boolean ignoreDescription, boolean ignoreWaypoints)
	{
		CalculateRouteRequest request = CalculateRouteRequest(start, destination);

		request.setCalculationMode(calculationMode);
		request.setVehicleType(vehicleType);
		request.setViaPoints(viaPoints);
		request.setDescriptionLanguage(descriptionLanguage);
		request.setIgnoreDescription(ignoreDescription);
		request.setIgnoreWaypoints(ignoreWaypoints);
		request.setLoadTMCs(false);

		return CalculateRoute(binding, map24id, clientid, request);
		
	} // CalculateRoute
	   

	/**
	* Helper method for instantiating a fetchRouteShape request.
	*
	* @param binding   The service that is used, for example ""http://maptp12.map24.com/map24/webservices1.5?soap=Map24Routing". 
	*                  If null, the server URL specified in the client stub Map24RoutingLocator is used.
	* @param Map24ID   The Map24 ID used for authentication.
	* @param ClientID  ID of the client that has initiated a request (SessionID).
	* @param RouteID	 ID of a previously calculated route. 
  * @return 				 An instance of CalculateRouteResponse containing the geometry (shape items) of the specified route.
	*/
	public static FetchRouteShapeResponse fetchRouteShape(String binding,
			String Map24ID, String ClientID, String RouteID) {
		RequestHeader header = new RequestHeader();
		header.setMap24ID(Map24ID);
		header.setClientID(ClientID);

		FetchRouteShapeRequest request = new FetchRouteShapeRequest();
		request.setRouteID(RouteID);

		try {
			return GetMap24RoutingPort(binding, header.getMap24ID(),
					header.getClientID()).fetchRouteShape(header, request);
		} catch (Exception e) {
			System.err.println(e);
		}

		return null;

	} // fetchRouteShape

	/**
	* Helper method for instantiating a LinkInfoRequest object.
	*
	* @param binding   The service that is used, for example ""http://maptp12.map24.com/map24/webservices1.5?soap=Map24Routing". 
	*                  If null, the server URL specified in the client stub Map24RoutingLocator is used.
	* @param Map24ID   The Map24 ID used for authentication.
	* @param ClientID  ID of the client that has initiated a request (SessionID).
	* @param Coordinate	  	The coordinate to find the link in the routing network for. The coordinate must 
	* be WGS 84 compliant and within the boundaries of the map. If the Map24 Routing Service does not find a 
	* link at the given coordinate, it will return the link that is closest to the given coordinate.
	* @param ped_routes 
	* @param routingID
	* @return An instance of LinkInfoResponse containing the links in the routing network that were found for the given coordinate.
	*/
	public static LinkInfoResponse getLinkInfo(String binding,
			String Map24ID, String ClientID, Coordinate Coordinate, boolean ped_routes, String routingID) {
		RequestHeader header = new RequestHeader();
		header.setMap24ID(Map24ID);
		header.setClientID(ClientID);

		LinkInfoRequest request = new LinkInfoRequest();
		
		if (ped_routes)
		{
		MProperty[] props= new MProperty[1];
		
		props[0]=	new MProperty();
		props[0].setKey("ped");
		props[0].setValue("1");	
		
		request.setRequestProperties(props);
		}
		
		request.setCoordinate(Coordinate);
		request.setRoutingServiceID(routingID);
		

		try {
			return GetMap24RoutingPort(binding, header.getMap24ID(),
					header.getClientID()).linkInfo(header, request);
		} catch (Exception e) {
			System.err.println(e);
		}

		return null;

	} // fetchRouteShape

	
} // Map24RoutingServiceWrapper
